//
//  ProvinceSelectedViewController.m
//  1gene
//
//  Created by 1jiyin on 16/4/11.
//  Copyright © 2016年 1jiyin. All rights reserved.
//

#import "ProvinceSelectedViewController.h"

@interface ProvinceSelectedViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,copy)NSMutableArray *provinceArray;

@end

@implementation ProvinceSelectedViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:[UIView new]];
    [self createNavigation];
    [self loadData];
    [self createUI];
    // Do any additional setup after loading the view.
}
-(void)createUI
{
    UITableView *table=[[UITableView alloc]initWithFrame:CGRectMake(0, 20+44, self.view.frame.size.width, self.view.frame.size.height-44-20) style:UITableViewStylePlain];
    table.delegate=self;
    table.dataSource=self;
    table.tableHeaderView=[UIView new];
    table.tableFooterView=[UIView new];
    table.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:table];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _provinceArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    for (id subView in cell.contentView.subviews) {
        [subView removeFromSuperview];
    }
    MyCityModel *model=_provinceArray[indexPath.row];
    
    UILabel *lab=[[UILabel alloc]initWithFrame:CGRectMake(5, 0, self.view.frame.size.width-5, 40-1)];
    lab.text=model.province;
    lab.textColor=[UIColor blackColor];
    lab.font=[UIFont systemFontOfSize:16];
    [cell.contentView addSubview:lab];
    
    UIView *line=[[UIView alloc]initWithFrame:CGRectMake(0, 39, self.view.frame.size.width, 1)];
    line.backgroundColor=[UIColor grayColor];
    [cell.contentView addSubview:line];
    
    
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}
-(void)loadData{
    NSString * filePath=[[NSBundle mainBundle] pathForResource:@"city"ofType:@"txt"];
    NSString * str = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    //将字符串转换成data格式
    NSData * data = [str dataUsingEncoding:NSUTF8StringEncoding];
    // JSON序列化
    NSArray * arr = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    NSLog(@"得到的地址%@",arr);
    _provinceArray = [NSMutableArray new];
    NSLog(@"===============%@",arr);
    if (arr) {
        for (NSDictionary * dic1 in arr) {
            MyCityModel *model=[MyCityModel new];
            model.province=dic1[@"name"];
            model.array=dic1[@"city"];
            [_provinceArray addObject:model];
        }
    }
    
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyCityModel *model=_provinceArray[indexPath.row];
    MyCityViewController *vc=[MyCityViewController new];
    if (vc.arr==nil) {
        vc.arr=model.array;
    }
    vc.province=model.province;
//    vc.backToChangeBaseViewController=^(NSString *city){
//        self.changeCity(city);
//        self.navigationController.navigationBar.hidden=YES;
//        [self.navigationController popViewControllerAnimated:YES];
//    };
    vc.myState=_myState;
    [self.navigationController pushViewController:vc animated:YES];
}


-(void)createNavigation
{
    self.navigationController.navigationBar.barTintColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];
    //返回
    UIButton *leftBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame=CGRectMake(0, 0, 25, 25);
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"left_back"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    
    //标题
    UILabel *title=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
    title.text=@"城市选择";
    title.font=[UIFont systemFontOfSize:22];
    title.textAlignment=NSTextAlignmentCenter;
    title.textColor=[UIColor whiteColor];
    self.navigationItem.titleView=title;
}
//返回上一页
-(void)leftBtnClick:(UIButton *)sender
{
    self.navigationController.navigationBar.hidden=YES;
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden=NO;
    [self.navigationController.navigationBar setTranslucent:YES];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [self.navigationController.navigationBar setTranslucent:NO];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
